package com.rzico.sbl.ui.statistic

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayoutMediator
import com.rzico.sbl.R
import com.rzico.sbl.databinding.FragmentStatisticBinding
import com.rzico.sbl.model.ManagerContent
import com.xinnuo.common.extend.get
import com.xinnuo.common.extend.startActivityEx
import com.xinnuo.common.view.FullyGridLayoutManager
import com.xinnuo.common.decoration.GridVerticalDecoration
import com.xinnuo.common_ui.base.NavigationFragment
import com.xinnuo.slimadapter.SlimAdapter

class StatisticFragment : NavigationFragment() {

    private var mBinding: FragmentStatisticBinding? = null

    override fun getViewModel() = get<StatisticViewModel>()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ) = FragmentStatisticBinding
        .inflate(inflater, container, false)
        .also { mBinding = it }
        .root

    override fun initTitle() {
        initTabLayout()
        initLayout()

        mAdapter.setDataList(getViewModel().commonIcons())
    }

    private fun initTabLayout() {
        mBinding?.apply {
            // 去除边缘阴影
            statisticVp.getChildAt(0).overScrollMode = View.OVER_SCROLL_NEVER

            // ViewPager2左右滑动时，默认不会预加载，Fragment生命周期
            // 未创建：onCreate → onStart → onPause(上一个Fragment) → onResume
            // 已创建：onPause(上一个Fragment) → onResume
            // 当点击tab时，会执行预加载，Fragment生命周期
            // 未创建时：预加载前一个(onCreate → onStart)
            //          → 当前Fragment(onCreate → onStart)
            //          → 预加载后一个(onCreate → onStart)
            //          → onPause(上一个Fragment) → onResume
            // 已创建：onPause(上一个Fragment) → onResume
            statisticVp.adapter =
                object : FragmentStateAdapter(requireActivity()) {
                    override fun getItemCount() = 2
                    override fun createFragment(position: Int) =
                        TotalFragment.newInstance(position)
                }

            TabLayoutMediator(statisticTab, statisticVp) { tab, positon ->
                tab.text = getViewModel().mTitleList[positon]
            }.attach()
        }
    }

    private fun initLayout() {
        mBinding?.let {
            it.statisticIcons.apply {
                isNestedScrollingEnabled = false
                layoutManager = FullyGridLayoutManager(getIContext(), 4)

                addItemDecoration(GridVerticalDecoration(vertical = 20f))

                mAdapter = SlimAdapter.creator()
                    .register<ManagerContent>(R.layout.item_home_icon) { jector, bean, _ ->

                        // 解构声明
                        val (title, imageId, _, _) = bean

                        jector.apply {
                            text(R.id.item_icon_title, title)
                            image(R.id.item_icon_img, imageId)

                            clicked {
                                when (title) {
                                    "押桶" -> startActivityEx<BucketHandleActivity>()
                                    "空桶" -> startActivityEx<BucketActivity>()
                                    "推广" -> startActivityEx<ShareActivity>()
                                    "记账" -> startActivityEx<BillActivity>()
                                    "送货" -> startActivityEx<SendActivity>()
                                    "水票" -> startActivityEx<TicketActivity>()
                                    "商品" -> startActivityEx<GoodsStatisticActivity>()
                                    "配送联盟" -> startActivityEx<UnionActivity>()
                                    "库存管理" -> startActivityEx<StockActivity>()
                                }
                            }
                        }
                    }
                    .attachTo(this)
            }
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mBinding = null
    }

}